package com.common.platform.base.config.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;

import javax.print.attribute.standard.Media;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;


@Configuration("defaultFastjsonConfig")
@ConditionalOnClass({JSON.class})
@ConditionalOnMissingBean({FastJsonHttpMessageConverter.class})
@ConditionalOnWebApplication
public class FastJsonAutoConfiguration {

    @Bean
    public CustomFastJsonHttpMessageConverter customFastJsonHttpMessageConverter(){
        CustomFastJsonHttpMessageConverter converter = new CustomFastJsonHttpMessageConverter();
        converter.setFastJsonConfig(fastjsonConfig());
        converter.setSupportedMediaTypes(getSupportedMediaType());
        ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
        return converter;

    }
    public FastJsonConfig fastjsonConfig(){
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat,SerializerFeature.WriteMapNullValue,SerializerFeature.DisableCircularReferenceDetect,SerializerFeature.BrowserCompatible);
        fastJsonConfig.setDateFormat("yyy-MM-dd HH:mm:ss");
        fastJsonConfig.setCharset(StandardCharsets.UTF_8);
        initOtherValueFilters(fastJsonConfig);
        return fastJsonConfig;
    }
    public List<MediaType> getSupportedMediaType(){
        ArrayList<MediaType> mediaTypes = new ArrayList<>();
        mediaTypes.add(MediaType.APPLICATION_PROBLEM_JSON);
        mediaTypes.add(MediaType.valueOf("application/vnd.spring-boot.actuator.v2+json"));
        return mediaTypes;
    }
    protected void initOtherValueFilters(FastJsonConfig fastJsonConfig){
        ValueFilter nullValueFilter =(object,name,value) -> (null ==value)?"":value;
        fastJsonConfig.setSerializeFilters(nullValueFilter);
    }

}
